package com.lkl.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lkl.entity.BaseResp;
import com.lkl.entity.TgHotel;
import com.lkl.dao.TgHotelDao;
import com.lkl.entity.TgHotelHome;
import com.lkl.service.TgHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * (TgHotel)表服务实现类
 *
 * @author makejava
 * @since 2022-03-31 15:53:15
 */
@Service("tgHotelService")
public class TgHotelServiceImpl implements TgHotelService {
    @Resource
    private TgHotelDao tgHotelDao;

    @Autowired
    RedisTemplate redisTemplate;

    static final String HOTEL = "HOTEL_";

    @Override
    public BaseResp findAll() {

        Boolean aBoolean = redisTemplate.hasKey(HOTEL);

        if (aBoolean) {

            Set set = redisTemplate.opsForZSet().reverseRange(HOTEL, 0, 10);
            Long size = redisTemplate.opsForZSet().size(HOTEL);

            return new BaseResp(1, "查询成功！", size, set);
        } else {

            List<TgHotel> all = tgHotelDao.findAll();

            Set set = null;
            Long size = null;

            if (all.size() > 0) {
                for (TgHotel tgHotel : all) {

                    redisTemplate.opsForZSet().add(HOTEL, tgHotel, tgHotel.getHotelCourse());
                }

                set = redisTemplate.opsForZSet().reverseRange(HOTEL, 0, 10);
                size = redisTemplate.opsForZSet().size(HOTEL);


            } else {

                redisTemplate.opsForZSet().add(HOTEL, "", 0.0);
                redisTemplate.expire(HOTEL, 20, TimeUnit.SECONDS);

            }
            return new BaseResp(1, "查询成功！", size, set);
        }
    }

    public BaseResp findByName(String hotelName) {

        if (hotelName == null && hotelName.equals("")) {
            return new BaseResp(0, "请输入查询的酒店名", null, null);
        }

        Boolean aBoolean = redisTemplate.hasKey(HOTEL + hotelName);

        if (aBoolean) {

            List range = redisTemplate.opsForList().range(HOTEL + hotelName, 0, -1);
            Long size = redisTemplate.opsForList().size(HOTEL + hotelName);

            return new BaseResp(1, "查询成功！", size, range);
        } else {

            List<TgHotel> all = tgHotelDao.findByName(hotelName);

            List list = null;
            Long size = null;

            if (all.size() > 0) {

                redisTemplate.opsForList().rightPushAll(HOTEL + hotelName, all);

                list = redisTemplate.opsForList().range(HOTEL + hotelName, 0, -1);
                size = redisTemplate.opsForList().size(HOTEL + hotelName);

            } else {
                redisTemplate.opsForList().rightPushAll(HOTEL + hotelName, "");
                redisTemplate.expire(HOTEL, 20, TimeUnit.SECONDS);
            }
            return new BaseResp(1, "酒店房间查询成功！", size, list);
        }
    }

    @Override
    public BaseResp findHomeByIdAndHotelId(Integer hotelId, Integer homeId) {
        if (hotelId == null) {
            return new BaseResp(0, "请输入id！", null, null);
        }
        if (homeId == null) {
            return new BaseResp(0, "请输入status！", null, null);
        }

        TgHotelHome tgHotelHome = tgHotelDao.findHomeByIdAndHotelId(hotelId, homeId);

        if (tgHotelHome == null) {
            return new BaseResp(0, "此条房间已经定完！", null, null);
        }
        return new BaseResp(1, "查询成功！", null, tgHotelHome);
    }

    @Override
    @Transactional
    public BaseResp updateHotelHomeNum(Integer num, Integer homeId) {

        if (num == null) {
            return new BaseResp(0, "请输入num！", null, null);
        }
        if (homeId == null) {
            return new BaseResp(0, "请输入status！", null, null);
        }

        int i = tgHotelDao.updateHotelHomeNum(num, homeId);

        if (i == 0) {
            return new BaseResp(0, "更新失败！！", null, null);
        }

        BaseResp byId = this.findHomeById(homeId);

        if (byId.getCode() == 0) {
            return byId;
        }
        String string = JSONObject.toJSONString(byId.getData());
        TgHotelHome tgHotelHome = JSONObject.parseObject(string, TgHotelHome.class);
        Integer hotelId = tgHotelHome.getHotelId();
        TgHotel tgHotel = tgHotelDao.findHotelById(hotelId);
        String hotelName = tgHotel.getHotelName();

        if (redisTemplate.hasKey(HOTEL)) {
            redisTemplate.delete(HOTEL);
        }

        if (redisTemplate.hasKey(HOTEL + hotelName)) {
            redisTemplate.delete(HOTEL + hotelName);
        }


        return new BaseResp(1, "更新成功！", null, null);
    }

    @Override
    public BaseResp updateHotelHomeStatus(Integer status, Integer homeId) {

        if (status == null) {
            return new BaseResp(0, "请输入status！", null, null);
        }
        if (homeId == null) {
            return new BaseResp(0, "请输入status！", null, null);
        }
        int i = tgHotelDao.updateHotelHomeStatus(status, homeId);

        if (i == 0) {
            return new BaseResp(0, "更新失败！！", null, null);
        }

        BaseResp byId = this.findHomeById(homeId);

        if (byId.getCode() == 0) {
            return byId;
        }
        String string = JSONObject.toJSONString(byId.getData());
        TgHotelHome tgHotelHome = JSONObject.parseObject(string, TgHotelHome.class);
        Integer hotelId = tgHotelHome.getHotelId();
        TgHotel tgHotel = tgHotelDao.findHotelById(hotelId);
        String hotelName = tgHotel.getHotelName();

        if (redisTemplate.hasKey(HOTEL)) {
            redisTemplate.delete(HOTEL);
        }

        if (redisTemplate.hasKey(HOTEL + hotelName)) {
            redisTemplate.delete(HOTEL + hotelName);
        }

        return new BaseResp(1, "更新成功！", null, null);

    }

    @Override
    public BaseResp findHomeById(Integer homeId) {

        TgHotelHome tgHotelHome = tgHotelDao.findHomeById(homeId);

        if (tgHotelHome == null) {
            return new BaseResp(0, "查不到信息！", null, null);
        }

        return new BaseResp(1, "成功！！", null, tgHotelHome);
    }
}